import { createI18n } from 'vue-i18n'
import cache from '@/utils/cache'
import { LANG_KEY } from '@/enums/cacheEnums'

// 动态读取本地语言
export async function getMessageList() {

    // 匹配views里面所有的.vue文件，动态引入
    const langJson = import.meta.glob('/src/lang/locale/**/*', { eager: true })
    const list: any = new Object;

    Object.keys(langJson).forEach((key) => {
        const name = key.replace(/(.*\/)*([^.]+).*/gi, '$2')
        const jsonData: any = langJson[key]
        list[`${name}`] = jsonData.default
    })
    return list;
}

const i18n = createI18n({
  legacy: false, // 解决控制台报错 Uncaught SyntaxError: Not available in legacy mode
//   globalInjection: true, //全局生效 $t
//   locale: cache.get(LANG_KEY) || 'zh-CN',
  locale: cache.get(LANG_KEY),
  fallbackLocale: 'en-US',
  messages: await getMessageList(),
  missingWarn: false,
  silentTranslationWarn: true, // 去掉警告
  silentFallbackWarn: true, //抑制警告
})

export default i18n